const Koa = require('koa')
const app = new Koa();
const router = require('koa-router')()
const views = require('koa-views')
const json = require('koa-json')
const onerror = require('koa-onerror')
const bodyparser = require('koa-bodyparser')
const logger = require('koa-logger')
// const koaBody = require('koa-body');
// app.use(koaBody({
//   multipart: true,
//   formidable: {
//     maxFileSize: 100 * 1024 * 1024    // 设置上传文件大小最大限制，默认2M
//   }
// }));
const index = require('./routes/index')
const users = require('./routes/users')
const zbf=require('./routes/zbf')
const cors = require("@koa/cors")
const formidable =require('koa-formidable')
app.use(cors())
//俊风聊天后台接口
const JFServeRouter = require('./routes/chatMsg')

const my = require('./routes/my')
const ymz = require("./routes/ymz")
// error handler
onerror(app)

// middlewares
app.use(bodyparser({
  enableTypes: ['json', 'form', 'text']
}))
// app.use(formidable({
//   // uploadDir:config.uploadDir,//配置上传路径
//   keepExtensions:true//设置该属性为true可以使得上传的文件保持原来的文展名件的扩
// }))
app.use(bodyparser());
app.use(json())
app.use(logger())
app.use(require('koa-static')(__dirname + '/public'))

app.use(views(__dirname + '/views', {
  extension: 'ejs'
}))


// logger
app.use(async (ctx, next) => {
  const start = new Date()
  await next()
  const ms = new Date() - start
  console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)
})


// routes
app.use(index.routes(), index.allowedMethods())
app.use(users.routes(), users.allowedMethods())
app.use(zbf.routes(), zbf.allowedMethods())
app.use(JFServeRouter.routes(), JFServeRouter.allowedMethods())
app.use(router.routes()).use(router.allowedMethods())
app.use(my.routes(), my.allowedMethods())
app.use(ymz.routes(), ymz.allowedMethods())

// error-handling
app.on('error', (err, ctx) => {
  console.error('server error', err, ctx)
});

module.exports = app
